package utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TSPSolution implements Cloneable {
    private int n;
    private double cost;
    private int[] permut;

    public TSPSolution(TSPSolution other) {
        setN(other.n);
        setCost(other.cost);
        permut = new int[n + 1];
        for (int i = 1; i <= n; i++)
            permut[i] = other.permut[i];
    }
    
    public TSPSolution(int n_) {
        List<Integer> list;
        setN(n_);
        list = new ArrayList<Integer>();
        for (int i = 1; i <= n; i++)
            list.add(i);
        Collections.shuffle(list);
        permut = new int[n + 1];
        int index = 0;
        for (int i: list) {
            index++;
            permut[index] = i;
        }
    }
    
    public TSPSolution(HamiltonCycle h) {
        setN(h.getN());
        permut = new int[n + 1];
        int index = 0;
        for (int i: h.getPath()) {
            index++;
            permut[index] = i;
            if (index == n)
                return;
        }        
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public int[] getPermut() {
        return permut;
    }

    public void setPermut(int[] permut) {
        this.permut = permut;
    }

    public int getN() {
        return n;
    }

    public void setN(int n) {
        this.n = n;
    }
    
    public int getCity(int i) {
        return permut[i];
    }

    public void setCity(int i, int city) {
        permut[i] = city;
    }
    
    public void display() {
        System.out.println("Cost: " + cost);
        for (int j = 1; j <= n; j++)
            System.out.print(getCity(j) + " -> ");
        System.out.println();        
    }

    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
